<template>
  <zh-form ref="appFormRef"  :model="state.form"  :rules="state.rules" label-width="140px">
    <zh-form-input prop="appId" :disabled="true" label="应用ID" v-model="state.form.appId"></zh-form-input>
    <zh-form-input prop="appName" :disabled="props.showFlag" label="应用名称" v-model="state.form.appName"></zh-form-input>
    <zh-form-select prop="type" :disabled="props.showFlag" label="应用类别" :options="state.typeDictData" placeholder="请选择" v-model="state.form.type"></zh-form-select>
    <zh-form-select prop="status" :disabled="props.showFlag" label="有效状态" :options="state.statusDictData" placeholder="请选择" v-model="state.form.status"></zh-form-select>
    <zh-form-date prop="year" label="年份" type="year"  :disabled="state.form.appId!=null&&state.form.appId!=''"
                  v-model="state.form.year"></zh-form-date>
    <zh-form-number
        prop="orderNo"
        label="排序号"
        placeholder="请输入排序号"
        v-model="state.form.orderNo"
        :disabled="props.showFlag"
    ></zh-form-number>
    <zh-form-select prop="publicFlag" :disabled="props.showFlag" label="是否公共应用" :options="state.flagDictData" placeholder="请选择" v-model="state.form.publicFlag"></zh-form-select>
    <zh-form-input prop="appKey" :span="24" show-password type="password" v-if="props.showFlag"  :clearable="false" label="appKey" v-model="state.form.appKey"></zh-form-input>
    <zh-form-input prop="appSecret" :span="24" show-password type="password" v-if="props.showFlag"  :clearable="false" label="appSecret" v-model="state.form.appSecret"></zh-form-input>
    <zh-form-select prop="keyAlgorithm" :disabled="props.showFlag" label="加密算法" :options="state.keyAlgorithmDictData" placeholder="请选择" v-model="state.form.keyAlgorithm"></zh-form-select>
    <zh-form-select prop="ckeckToken" :disabled="props.showFlag" label="是否校验token" :options="state.flagDictData" placeholder="请选择" v-model="state.form.ckeckToken"></zh-form-select>
    <zh-form-select prop="checkScope" :disabled="props.showFlag" label="是否校验权限" :options="state.flagDictData" placeholder="请选择" v-model="state.form.checkScope"></zh-form-select>
    <zh-form-select prop="signFlag" :disabled="props.showFlag" label="是否校验签名" :options="state.flagDictData" placeholder="请选择" v-model="state.form.signFlag"></zh-form-select>
    <zh-form-select prop="encryptionFlag" :disabled="props.showFlag" label="是否校验加密" :options="state.flagDictData" placeholder="请选择" v-model="state.form.encryptionFlag"></zh-form-select>
    <zh-form-date :disabled="props.showFlag" prop="clientSecretExpiresAt" label="秘钥有效截止时间" placeholder="" type="datetime" v-model="state.form.clientSecretExpiresAt"></zh-form-date>
    <zh-form-number
        prop="qps"
        label="并发数"
        placeholder="请输入应用单个接口调用默认并发数"
        v-model="state.form.qps"
        :disabled="props.showFlag"
    ></zh-form-number>
    <zh-form-input
        prop="tokenTime"
        label="token有效时长"
        placeholder="请输入接口调用token有效时长"
        v-model="state.form.tokenTime"
        :disabled="props.showFlag"
    >
      <template #append>秒</template>
    </zh-form-input>
    <zh-form-input
        prop="refreshTokenTime"
        label="刷新token有效时长"
        placeholder="请输入刷新token有效时长"
        v-model="state.form.refreshTokenTime"
        :disabled="props.showFlag"
    >
      <template #append>秒</template>
    </zh-form-input>
    <zh-form-input :span="24" :disabled="props.showFlag" :rows="4" prop="whiteIp" type="textarea" placeholder="多个用英文逗号隔开" label="ip白名单" v-model="state.form.whiteIp"></zh-form-input>
    <zh-form-select :disabled="props.showFlag" prop="authStatus" label="审核状态" :options="state.dictData.authStatus" v-model="state.form.authStatus"></zh-form-select>
    <zh-form-date :disabled="props.showFlag" prop="authTime" label="审核时间" placeholder="" type="datetime" v-model="state.form.authTime"></zh-form-date>
    <zh-form-choose-user :disabled="props.showFlag" prop="authUserId" label="审核人" v-model="state.form.authUserId"></zh-form-choose-user>
    <zh-form-input
        :disabled="props.showFlag"
        :span="24"
        :rows="4"
        prop="authRemarks"
        type="textarea"
        label="审核意见"
        v-model="state.form.authRemarks"
    ></zh-form-input>
    <zh-form-input :span="24" :disabled="props.showFlag" :rows="4" prop="remarks" type="textarea" label="应用描述" v-model="state.form.remarks"></zh-form-input>

  </zh-form>
</template>
<script lang="ts" setup name="FormClassForm">
import { ElMessage } from 'element-plus';
import moment from 'moment'
//------------------接收传入参数(组件)-------------------
const props = defineProps({
  appInfo: {
    type: Object,
    default: () => {},
    required: false,
  },
  showFlag:{
    type:Boolean,
    default:false
  },
  modelValue: Boolean,
});
//------------------定义子组件---------------------------
const appFormRef=ref()
const emit = defineEmits(['update:modelValue']);
//------------------内部变量定义-------------------------
const state=reactive({
  form:{type: '1',status:'1',keyAlgorithm:'SM2',publicFlag:'0',ckeckToken:'1',checkScope:'1',authStatus:'1',year: moment().format('YYYY'),signFlag:'1',encryptionFlag:'0',qps:3,tokenTime:1800,refreshTokenTime:86400} as any,
  statusDictData:[{dictKey:'1',dictValue:'有效'},{dictKey:'0',dictValue:'无效'}],
  typeDictData:[{dictKey:'1',dictValue:'电脑端应用'},{dictKey:'2',dictValue:'手机端应用'}],
  flagDictData:[{dictKey:'1',dictValue:'是'},{dictKey:'0',dictValue:'否'}],
  keyAlgorithmDictData:[{dictKey:'SM2',dictValue:'国密非对称加密'},{dictKey:'SM4',dictValue:'国密对称加密'},{dictKey:'RSA',dictValue:'RSA非对称加密'},{dictKey:'AES',dictValue:'AES对称加密'}],
  dictData:{} as any,
  rules:{
    appName: [
      {
        required: true,
        message: '请输入应用名称',
        trigger: 'blur',
      },
    ],
    status: [
      {
        required: true,
        message: '请选择应用状态',
        trigger: 'blur',
      },
    ],
    year: [
      {
        required: true,
        message: '请选择年度',
        trigger: 'blur',
      },
    ],
    authStatus: [
      {
        required: true,
        message: '请选择审核状态',
        trigger: 'blur',
      },
    ],
    ckeckToken: [
      {
        required: true,
        message: '请选择是否校验token',
        trigger: 'blur',
      },
    ],
    checkScope: [
      {
        required: true,
        message: '请选择是否校验权限',
        trigger: 'blur',
      },
    ],
    signFlag: [
      {
        required: true,
        message: '请选择是否需要平台签名',
        trigger: 'blur',
      },
    ],
    encryptionFlag: [
      {
        required: true,
        message: '请选择是否需要平台加密',
        trigger: 'blur',
      },
    ],
    publicFlag: [
      {
        required: true,
        message: '请选择是否公开应用',
        trigger: 'blur',
      },
    ],
  },
})
const init=()=>{
  const params = {
    dictFields: 'authStatus',
  };
  $api.common.getBatchDict.post(params).then((res) => {
    state.dictData = res.result;
  });
}
onMounted(()=>{
  init()
})
if (props.appInfo) {
  state.form=Object.assign(props.appInfo.appId?{}:{type: '1',status:'1',keyAlgorithm:'SM2',publicFlag:'0',ckeckToken:'1',checkScope:'1',authStatus:'1',year: moment().format('YYYY'),signFlag:'1',encryptionFlag:'0',qps:3,tokenTime:1800,refreshTokenTime:86400},props.appInfo,{year:props.appInfo.year+""})

}
async function save() {
  //校验表单
  let checkFlag = await appFormRef.value.checkForm();
  if (checkFlag) {
    const info = state.form;
    let ret = null as any;
    if (info.appId) {
      ret = await $api.common.send.post('application0005', info);
      ret.saveType = 'edit';
      if (ret.code === 0) {
        ElMessage.success('修改成功！');
        emit('update:modelValue', false);
      }
    } else {
      ret = await $api.common.send.post('application0004', info);
      ret.saveType = 'add';
      if (ret.code === 0) {
        ElMessage.success('新建成功！');
        emit('update:modelValue', false);
      }
    }
    return ret;
  }
  return null;
}
//------------设置给父组件调用的数据/方法(组件)-----------
defineExpose({ form: state.form ,save});
</script>